﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_AddPosition]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_AddPosition];
GO
CREATE PROCEDURE [dbo].[sproc_AddPosition]
    @Position_Name nvarchar(300),
    @Position_Remark nvarchar(300),
    @Position_ParentID int
/*

============================================================
功能:    增加职位
参数:
    @Position_Name nvarchar(300)        :    职位名称
    @Position_Remark nvarchar(300)    :    职位简介
    @Position_ParentID int        :    上级职位ID
============================================================

*/
AS
--父接点小于等于零时，为跟接点
SET NOCOUNT ON
IF @Position_ParentID<=0
BEGIN
    INSERT INTO uds_Position (Super_Position_ID,Position_Name,Position_Description)
        VALUES(0,@Position_Name,@Position_Remark)
    UPDATE uds_Position SET Super_Position_ID = Position_ID 
        WHERE Position_ID = @@IDENTITY 
    SELECT @@IDENTITY AS Position_ID
    RETURN @@IDENTITY
END
ELSE
BEGIN
    --判断是否存在父接点
    IF EXISTS(SELECT 1 FROM uds_Position WHERE Position_id = @Position_ParentID)
    BEGIN
        INSERT INTO uds_Position (Super_Position_ID,Position_Name,Position_Description)
        VALUES(@Position_ParentID,@Position_Name,@Position_Remark)
    
        SELECT @@IDENTITY AS Position_ID
            RETURN @@IDENTITY
    END
    ELSE
        SELECT 0 AS Position_id
        RETURN 0
END    
SET NOCOUNT OFF